package com.esm.sm800.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.adapter.DemoAdapter;
import com.esm.sm800.database.manager.MemberCenter;
import com.esm.sm800.database.manager.SystemCenter;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.model.Entry;
import zuo.biao.library.util.EditTextUtil;

//人员管理
public class RyglActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "RyglActivity";

    private ListView lv_hpcode;
    TextView tv_num;
    EditText et_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_rygl);
        findViewById(R.id.tv_back).setOnClickListener(this);
        findViewById(R.id.tv_add).setOnClickListener(this);
        findViewById(R.id.tv_search).setOnClickListener(this);
        findViewById(R.id.et_name).setOnClickListener(this);
        lv_hpcode = (ListView) findViewById(R.id.lv_hpcode);
        tv_num = (TextView) findViewById(R.id.tv_num);
        et_name = (EditText) findViewById(R.id.et_name);
        et_name.setFocusable(false);

        MemberCenter.getInstance().memberlist = MemberCenter.getInstance().getMemberlist("");
        updateData();
        adapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                onSelect(pos);
            }
        });
    }

    private void onSelect(int pos) {
        if (pos < 0 || pos >= MemberCenter.getInstance().memberlist.size()) {
            showShortToast(getString(R.string.xzchuowu));
            return;
        }
        SystemCenter.getInstance().memberEdit = MemberCenter.getInstance().memberlist.get(pos);
        Intent intent = new Intent(this, RyxzActivity.class);
        startActivityForResult(intent, 61);
    }

    private void reset() {
        updateData();
    }

    void updateData() {
        setList(getList());
        tv_num.setText(getString(R.string.list_total) + "  " + demodataList.size());
    }

    private DemoAdapter adapter;

    private void setList(List<Entry<Integer, String>> list) {
        if (list == null) {
            return;
        }
        if (adapter == null) {
            adapter = new DemoAdapter(context);
            lv_hpcode.setAdapter(adapter);
        }
        adapter.refresh(list);
    }

    private List<Entry<Integer, String>> demodataList;

    protected List<Entry<Integer, String>> getList() {
        demodataList = new ArrayList<Entry<Integer, String>>();
        for (int i = 0; i < MemberCenter.getInstance().memberlist.size(); i++) {
            demodataList.add(new Entry<Integer, String>(i, ""));
        }
        return demodataList;
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {

            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_add:
                onAdd();
                break;

            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_search:
                onSearch();
                break;
            case R.id.et_name:
                EditTextUtil.showKeyboard(context, et_name,true);
                break;


        }
    }

    private void onSearch() {
        String name = et_name.getText().toString();
        MemberCenter.getInstance().memberlist = MemberCenter.getInstance().getMemberlist(name);
        updateData();
    }

    private void onAdd() {
        SystemCenter.getInstance().memberEdit = null;
        Intent intent = new Intent(this, RyxzActivity.class);
        startActivityForResult(intent, 60);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        et_name.setText("");
        MemberCenter.getInstance().memberlist = MemberCenter.getInstance().getMemberlist("");
        updateData();
    }


    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}
